package br.net.woodstock.tickettoride.vaadin.web.access;

import java.util.Collection;

import br.net.woodstock.tickettoride.api.access.UserService;
import br.net.woodstock.tickettoride.orm.User;
import br.net.woodstock.tickettoride.orm.UserStatus;
import br.net.woodstock.tickettoride.orm.UserType;
import br.net.woodstock.tickettoride.vaadin.web.AbstractView;
import br.net.woodstock.tickettoride.vaadin.web.TicketToRideApplication;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class UserView extends AbstractView {

	private static final long	serialVersionUID	= -1727135237986118900L;

	private TextField			searchText;

	private TextField			login;

	private TextField			name;

	private TextField			email;

	private PasswordField		password;

	private NativeSelect		status;

	private NativeSelect		type;

	private Button				searchButton;

	private Button				newButton;

	private Table				table;

	private Window				editWindow;

	public UserView() {
		super();
	}

	@Override
	protected void init() {
		// Search forms
		Panel panel = new Panel();

		this.searchText = new TextField(this.getMessage("label.name"));
		this.searchText.setMaxLength(100);

		this.searchButton = new Button(this.getMessage("label.search"));
		this.newButton = new Button(this.getMessage("label.new"));

		panel.addComponent(this.searchText);
		panel.addComponent(this.searchButton);
		panel.addComponent(this.newButton);
		panel.setWidth(250, Sizeable.UNITS_PIXELS);

		this.searchButton.addListener(new ClickListener() {

			private static final long	serialVersionUID	= 1L;

			@Override
			public void buttonClick(final ClickEvent event) {
				TicketToRideApplication application = (TicketToRideApplication) UserView.this.getApplication();
				UserService userService = application.getUserService();
				Collection<User> users = userService.listByFilter((String) UserView.this.getSearchText().getValue());
				Table table = UserView.this.getTable();
				table.setContainerDataSource(UserView.this.toContainer(users));
				table.setColumnHeaders(new String[] { UserView.this.getMessage("label.login"), UserView.this.getMessage("label.name"), UserView.this.getMessage("label.email") });
				table.setEnabled(true);
				table.setVisible(true);
			}

		});

		this.table = new Table();
		this.table.setColumnCollapsingAllowed(true);
		this.table.setColumnReorderingAllowed(true);
		this.table.setSelectable(true);
		this.table.setEnabled(false);
		this.table.setVisible(false);
		this.table.setWidth(500, Sizeable.UNITS_PIXELS);
		this.table.setPageLength(10);
		this.table.setMultiSelect(false);
		this.table.setImmediate(true);
		this.table.setColumnReorderingAllowed(false);
		this.table.setColumnCollapsingAllowed(false);

		this.table.addListener(new ValueChangeListener() {

			private static final long	serialVersionUID	= 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				Object itemId = event.getProperty().getValue();
				if (itemId != null) {
					TicketToRideApplication application = (TicketToRideApplication) UserView.this.getApplication();
					UserService userService = application.getUserService();
					User user = userService.getById((Integer) itemId);
					UserView.this.getLogin().setValue(user.getLogin());
					UserView.this.getName().setValue(user.getName());
					UserView.this.getEmail().setValue(user.getEmail());
					UserView.this.getStatus().setValue(user.getStatus());
					UserView.this.getType().setValue(user.getType());
					UserView.this.getWindow().setVisible(true);
				}
			}

		});

		this.addComponent(panel);
		this.setComponentAlignment(panel, Alignment.TOP_CENTER);

		this.addComponent(this.table);
		this.setComponentAlignment(this.table, Alignment.TOP_CENTER);

		// Edit Window
		this.editWindow = new Window(this.getMessage("label.user"));
		this.editWindow.setModal(true);
		this.editWindow.setVisible(false);
		this.editWindow.setContent(new VerticalLayout());

		this.login = new TextField(this.getMessage("label.login"));
		this.login.setMaxLength(20);
		this.login.setRequired(true);
		this.editWindow.getContent().addComponent(this.login);

		this.name = new TextField(this.getMessage("label.name"));
		this.name.setMaxLength(100);
		this.name.setRequired(true);
		this.editWindow.getContent().addComponent(this.name);

		this.email = new TextField(this.getMessage("label.email"));
		this.email.setMaxLength(100);
		this.email.setRequired(true);
		this.editWindow.getContent().addComponent(this.email);

		this.password = new PasswordField(this.getMessage("label.password"));
		this.password.setMaxLength(20);
		this.password.setRequired(true);
		this.editWindow.getContent().addComponent(this.password);

		this.status = new NativeSelect(this.getMessage("label.status"));
		this.status.setNullSelectionAllowed(false);
		for (UserStatus status : UserStatus.values()) {
			this.status.addItem(status);
		}
		this.editWindow.getContent().addComponent(this.status);

		this.type = new NativeSelect(this.getMessage("label.type"));
		this.type.setNullSelectionAllowed(false);
		for (UserType type : UserType.values()) {
			this.type.addItem(type);
		}
		this.editWindow.getContent().addComponent(this.type);
		this.getMainWindow().addWindow(this.editWindow);
	}

	protected Container toContainer(final Collection<User> users) {
		IndexedContainer container = new IndexedContainer();
		container.addContainerProperty("login", String.class, null);
		container.addContainerProperty("name", String.class, null);
		container.addContainerProperty("email", String.class, null);

		for (User user : users) {
			Item item = container.addItem(user.getId());
			item.getItemProperty("login").setValue(user.getLogin());
			item.getItemProperty("name").setValue(user.getName());
			item.getItemProperty("email").setValue(user.getEmail());
		}
		return container;
	}

	public TextField getSearchText() {
		return this.searchText;
	}

	public TextField getLogin() {
		return this.login;
	}

	public TextField getName() {
		return this.name;
	}

	public TextField getEmail() {
		return this.email;
	}

	public PasswordField getPassword() {
		return this.password;
	}

	public NativeSelect getStatus() {
		return this.status;
	}

	public NativeSelect getType() {
		return this.type;
	}

	public Window getEditWindow() {
		return this.editWindow;
	}

	public Button getSearchButton() {
		return this.searchButton;
	}

	public Button getNewButton() {
		return this.newButton;
	}

	public Table getTable() {
		return this.table;
	}

	@Override
	protected String getMessage(String key) {
		return super.getMessage(key);
	}

}
